package com.daxidi.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.daxidi.cn.entity.Member;
import com.daxidi.cn.entity.User;
import com.daxidi.cn.mapper.UserMapper;
import com.daxidi.cn.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通过微信小程序登录时，记录微信小程序的信息，在表中增加用户注册类型
 * 服务实现类
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void deleteUserById(Integer id) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        userMapper.delete(wrapper);
    }

    @Override
    public void updateUserById(User user) {
        userMapper.updateById(user);
    }

    @Override
    public Map selectUsersPage(Integer pageNum, Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        // 调用mypatis-plus分页查询方法   获取分页后的结果
        Page<User> userPage = userMapper.selectPage(page, null);
        // 查询数据
        List<User> users = userPage.getRecords();
        // 总条数
        long total = userPage.getTotal();
        HashMap map = new HashMap();
        // 数据
        map.put("data", users);
        // 总条数
        map.put("count", total);
        System.out.println(users);
        return map;
    }

    public Long selectMemberIdByOpenId(String openId) {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("open_id", openId);
        List<User> users = userMapper.selectByMap(columnMap);
        if (users.size() > 0) {
            return users.get(0).getMembeId();
        } else {
            return null;
        }
    }

    // 根据memberId查询openId
    @Override
    public Long selectClerkIdByOpenId(String openId) {
        QueryWrapper<User> columnMap = new QueryWrapper<>();
        columnMap.eq("open_id", openId);
        List<User> users = userMapper.selectList(columnMap);
        for (User user : users) {
            if (null != user.getClerkId()){
                return user.getClerkId();
            }
        }
        return null;
    }

    @Override
    public User selectUserByOpenId(String openId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("open_id", openId);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public String selectOpenIdByMemberId(Long memberId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("membe_id", memberId);
        User user = userMapper.selectOne(wrapper);
        return user.getOpenId();
    }

    //  根据memberId修改user表信息
    @Override
    public void updateUserByMemberId(User user, Integer userId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", userId);
        userMapper.update(user, wrapper);
    }

    @Override
    public void updateUserByClerkId(User user, Integer userId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", userId);
        userMapper.update(user, wrapper);
    }

    // 根据memberId查询user表信息
    @Override
    public User selectUserByMemberId(Long memberId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("membe_id", memberId);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

}
